package com.open.rbac.service.sys.impl;

import java.util.List;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.open.center.common.util.CopyUtils;
import com.open.rbac.model.dto.sys.RoleUserRelDto;
import com.open.rbac.model.entity.sys.RoleUserRel;
import com.open.rbac.model.vo.sys.RoleUserRelVo;
import com.open.rbac.service.sys.IRoleUserRelService;
import com.open.rbac.service.sys.repository.RoleUserRelRepository;

/**
 * 角色与用户关系服务
 *
 * @author Riche's
 * @since 2025/9/2
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class RoleUserRelServiceImpl implements IRoleUserRelService {

    private final RoleUserRelRepository roleUserRelRepository;

    @Transactional
    @Override
    public void create(List<RoleUserRelVo> relVos) {
        List<RoleUserRel> rels = relVos.stream().map(relVo -> {
            RoleUserRel rel = new RoleUserRel();
            rel.setRoleId(relVo.getRoleId());
            rel.setUserId(relVo.getUserId());
            return rel;
        }).toList();
        roleUserRelRepository.saveAll(rels);
    }

    @Transactional
    @Override
    public void deleteReallyByRoleId(String roleId) {
        roleUserRelRepository.deleteReallyByRoleId(roleId);
    }

    @Transactional
    @Override
    public void deleteReallyByUserId(String userId) {
        roleUserRelRepository.deleteReallyByUserId(userId);
    }

    @Override
    public List<RoleUserRelDto> listByUserIds(List<String> userIds) {
        return CopyUtils.listCopy(roleUserRelRepository.findByUserIds(userIds), RoleUserRelDto.class);
    }
}
